package view.tool;

import controller.ChangePieceListener;
import controller.TempoChangeListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.*;

/**
 * Tool for changing properties of a Piece.
 * Current is can change the tempo and length of the piece.
 */
public class EffectsTool extends Tool {

	private JSlider tempoSlider;
	private JSpinner lengthSpinner;
	private JButton applyLengthButton;
	private JButton resetLengthButton;

	public EffectsTool(int initialTempo, int initialLength) {
		this.setLayout(new BorderLayout());
		this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		this.setOpaque(false);

		JPanel tempoPanel = new JPanel(new BorderLayout());
		tempoPanel.setOpaque(false);

	//Tempo Slider
		tempoSlider = new JSlider(SwingConstants.VERTICAL, 100, 1900, initialTempo);
		tempoSlider.setMajorTickSpacing(1700);
		tempoSlider.setMinorTickSpacing(1700/4);
		tempoSlider.setPaintTicks(true);
		tempoSlider.setOpaque(false);
		tempoSlider.setName("TEMPO_SLIDER");
		JLabel tempoLabel = new JLabel("Tempo");
		tempoLabel.setLabelFor(tempoSlider);
		tempoLabel.setForeground(Color.WHITE);
		tempoLabel.setHorizontalTextPosition(SwingConstants.CENTER);

		tempoPanel.add(tempoSlider, BorderLayout.CENTER);
		tempoPanel.add(tempoLabel, BorderLayout.SOUTH);


		JPanel lengthPanel = new JPanel(new BorderLayout(4,4));
		lengthPanel.setOpaque(false);

	//Length Spinner
		SpinnerModel model = new SpinnerNumberModel(initialLength, 10, 1000, 1);
		lengthSpinner = new JSpinner(model);

		JPanel lengthButtonPanel = new JPanel(new GridLayout(0,1));
		applyLengthButton = new JButton("Apply");
		applyLengthButton.setActionCommand(ChangePieceListener.CHANGE_LENGTH);
		resetLengthButton = new JButton("Reset");
		resetLengthButton.setActionCommand(ChangePieceListener.RESET_LENGTH);
		lengthButtonPanel.add(applyLengthButton);
		lengthButtonPanel.add(resetLengthButton);

		JLabel lengthLabel = new JLabel("Length");
		lengthLabel.setForeground(Color.WHITE);
		lengthPanel.add(lengthLabel, BorderLayout.NORTH);
		lengthPanel.add(lengthSpinner, BorderLayout.CENTER);
		lengthPanel.add(lengthButtonPanel, BorderLayout.EAST);

		//Add panels to effect panel
		this.add(tempoPanel, BorderLayout.WEST);
		this.add(Box.createHorizontalStrut(4), BorderLayout.CENTER);
		this.add(lengthPanel, BorderLayout.EAST);
	}

	public void setLengthValue(int value) {
		lengthSpinner.setValue(value);
	}

	public int getLengthValue() {
		return (int)lengthSpinner.getModel().getValue();
	}

	public void setTempoValue(int value) {
		tempoSlider.setValue(value);
	}

	public void setTempoChangeListener(TempoChangeListener tcl) {
		this.tempoSlider.addChangeListener(tcl);
	}

	public void setChangePieceListener(ChangePieceListener cpl) {
		applyLengthButton.addActionListener(cpl);
		resetLengthButton.addActionListener(cpl);
	}
}
